<?php

require_once('../../connection/_dbconnect_.php');
require_once('m_activity_log.php');

function getShift($id){

	$fetchedShift = array();
	
	$sql = "select idshift_schedule,name,start_time,end_time,description from shift_schedule where idshift_schedule='".$id."'";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	while($obj = mysql_fetch_object($result)){
		$fetchedShift[] = $obj->idshift_schedule;
		$fetchedShift[] = $obj->name;
		$fetchedShift[] = $obj->start_time;
		$fetchedShift[] = $obj->end_time;
		$fetchedShift[] = $obj->description;
	}
	
	return $fetchedShift;
}

function getShifts(){	
	$parentArray = array();	
	$sql = "select idshift_schedule,name,start_time,end_time,description from shift_schedule order by start_time ASC";	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	while($obj = mysql_fetch_object($result)){
		$childArray = array();		
		$childArray[] = $obj->idshift_schedule;
		$childArray[] = $obj->name;
		$childArray[] = $obj->start_time;
		$childArray[] = $obj->end_time;
		$childArray[] = $obj->description;		
		$parentArray[]= $childArray;
	}
	return $parentArray;
}


function addShift($addname,
				 $addstart_time,
				 $addend_time,
				 $adddescription){
				 
	$sql = "insert into shift_schedule values (NULL,
											   '$addname',
											   '$addstart_time',
											   '$addend_time',
											   '$adddescription');";	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else
		return true;
}

function saveShiftEdit($save_idshift_schedule,
					  $savename,
					  $savestart_time,
					  $saveend_time,
					  $savedescription){
	
	$sql = "update shift_schedule set name='".$savename."',
									  start_time='".$savestart_time."',
									  end_time='".$saveend_time."',
									  description='".$savedescription."'
									  where idshift_schedule='".$save_idshift_schedule."';";
	
	$result = mysql_query($sql) or die("Query: $sql \n\n".mysql_error());
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Shift=".$save_idshift_schedule." was modified");	
		return true;
	}
}

function deleteShift($idshift){
	
	$sql = "delete from shift_schedule where idshift_schedule='".$idshift."';";
	
	$result = mysql_query($sql);
	
	if (!$result) {
		return false;
	} else {
		$date =new DateTime('NOW');		
		$result=addLogEntry($date->format('Y-m-d'), "Shift=".$idshift." was deleted");	
		return true;
	}
}

?>